#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back
#define y1 y_alpha_1

using namespace std;

using ll = long long;
using db = double;
using vec = vector<int>;
using pii = pair<int,int>;

int read() {
	int x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-',ch = getchar();
	while(isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48),ch = getchar();
	return f ? -x : x;
}

mt19937 rnd(19491001);

const int N = 1e5 + 10;

int n;

namespace treap {
	const int V = 1e9 + 7;
	int pri[N],ch[N][2],val[N],ndn,siz[N],root;
	int newnode(int x) { pri[++ndn] = rnd() % V; ch[ndn][0] = ch[ndn][1] = 0; val[ndn] = x; siz[ndn] = 1; return ndn; }
	void pushup(int x) { siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + 1; } 
	void split(int cur,int k,int &x,int &y) {
		if(!cur) return x = y = 0, void();
		if(val[cur] <= k) x = cur,split(ch[cur][1],k,ch[cur][1],y),pushup(x);
		else y = cur,split(ch[cur][0],k,x,ch[cur][0]),pushup(y);
	}
	int merge(int x,int y) {
		if(!x || !y) return x | y;
		if(pri[x] < pri[y]) return ch[x][1] = merge(ch[x][1],y),pushup(x),x;
		else return ch[y][0] = merge(x,ch[y][0]),pushup(y),y;
	}
	int k_th(int x,int k) {
		while(true) {
			if(siz[ch[x][0]] + 1 == k) return x;
			if(k > siz[ch[x][0]] + 1) k -= siz[ch[x][0]] + 1,x = ch[x][1];
			else x = ch[x][0];
		} 
	}
	void insert(int x) { int a,b; split(root,x,a,b); root = merge(merge(a,newnode(x)),b); }
	void del(int x) { int a,b,c,d; split(root,x,a,b); split(a,x - 1,c,d); d = merge(ch[d][0],ch[d][1]); root = merge(merge(c,d),b); }
	int getrank(int x) { int a,b; split(root,x - 1,a,b); int ans = siz[a] + 1; root = merge(a,b); return ans; }
	int prev(int x) { int a,b; split(root,x - 1,a,b); int ans = val[k_th(a,siz[a])]; root = merge(a,b); return ans; }
	int nxtv(int x) { int a,b; split(root,x,a,b); int ans = val[k_th(b,1)]; root = merge(a,b); return ans; }
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	n = in;
	for(int i = 1;i <= n;i++) {
		int op = in,x = in;
		if(op == 1) treap :: insert(x);
		if(op == 2) treap :: del(x);
		if(op == 3) printf("%d\n",treap :: getrank(x));
		if(op == 4) printf("%d\n",treap :: val[treap :: k_th(treap :: root,x)]);
		if(op == 5) printf("%d\n",treap :: prev(x));
		if(op == 6) printf("%d\n",treap :: nxtv(x));
	}
	return 0;
}
